package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.qy.dao.PayInfoDao;
import java.util.List;
import java.util.Map;
import java.sql.Types;
import com.qy.db.DbParameter;
import java.util.HashMap;
import java.util.ArrayList;
import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.PayInfo;
import com.qy.db.BaseDao;

/**
* u_pay_info daoImpl实现类的生成
* @author zhangwenlong
* @date Thu Feb 08 15:07:42 GMT+08:00 2018
*/ 
public class PayInfoDaoImpl extends BaseDao  implements PayInfoDao {

	@Override
	public boolean createPayInfo(PayInfo payInfo) {
		boolean result = false;
		int id = -1;
		if (payInfo.beginAdd()) {
			String sql = "insert into u_pay_info (id,order_num,local_order_num,order_type,order_id,user_id,order_price,voucher_id,pay_status,create_date,complete_date) values (?,?,?,?,?,?,?,?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, payInfo.getId()));
			param.put(2, new DbParameter(Types.VARCHAR, payInfo.getOrderNum()));
			param.put(3, new DbParameter(Types.VARCHAR, payInfo.getLocalOrderNum()));
			param.put(4, new DbParameter(Types.INTEGER, payInfo.getOrderType()));
			param.put(5, new DbParameter(Types.INTEGER, payInfo.getOrderId()));
			param.put(6, new DbParameter(Types.INTEGER, payInfo.getUserId()));
			param.put(7, new DbParameter(Types.INTEGER, payInfo.getOrderPrice()));
			param.put(8, new DbParameter(Types.INTEGER, payInfo.getVoucherId()));
			param.put(9, new DbParameter(Types.INTEGER, payInfo.getPayStatus()));
			param.put(10, new DbParameter(Types.TIMESTAMP, payInfo.getCreateDate()));
			param.put(11, new DbParameter(Types.TIMESTAMP, payInfo.getCompleteDate()));
			id = execLastId(sql, param);
			result = id > -1;
			payInfo.commitAdd(result);
			payInfo.setId(id);
		}
		return result;
	}

	@Override
	public List<PayInfo> getPayInfoList() {
		String sql = "select * from u_pay_info;";
		PreparedStatement pstmt = execQuery(sql, null);
		ResultSet rs = null;
		List<PayInfo> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<PayInfo>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public PayInfo getPayInfoById(int id) {
		String sql = "select * from u_pay_info where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		PayInfo payInfo = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					payInfo = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return payInfo;
	}

	@Override
	public boolean updatePayInfo(PayInfo payInfo) {
		boolean result = false;
		if (payInfo.beginUpdate()) {
			String sql = "update u_pay_info set order_num=?,local_order_num=?,order_type=?,order_id=?,user_id=?,order_price=?,voucher_id=?,pay_status=?,create_date=?,complete_date=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.VARCHAR, payInfo.getOrderNum()));
			param.put(2, new DbParameter(Types.VARCHAR, payInfo.getLocalOrderNum()));
			param.put(3, new DbParameter(Types.INTEGER, payInfo.getOrderType()));
			param.put(4, new DbParameter(Types.INTEGER, payInfo.getOrderId()));
			param.put(5, new DbParameter(Types.INTEGER, payInfo.getUserId()));
			param.put(6, new DbParameter(Types.INTEGER, payInfo.getOrderPrice()));
			param.put(7, new DbParameter(Types.INTEGER, payInfo.getVoucherId()));
			param.put(8, new DbParameter(Types.INTEGER, payInfo.getPayStatus()));
			param.put(9, new DbParameter(Types.TIMESTAMP, payInfo.getCreateDate()));
			param.put(10, new DbParameter(Types.TIMESTAMP, payInfo.getCompleteDate()));
			param.put(11, new DbParameter(Types.INTEGER, payInfo.getId()));
			result = execNoneQuery(sql, param) > -1;
			payInfo.commitUpdate(result);
		}
		return result;
	}

	public PayInfo resultTo(ResultSet rs) throws SQLException {
		PayInfo payInfo = new PayInfo();
		payInfo.setId(rs.getInt("id"));
		payInfo.setOrderNum(rs.getString("order_num"));
		payInfo.setLocalOrderNum(rs.getString("local_order_num"));
		payInfo.setOrderType(rs.getInt("order_type"));
		payInfo.setOrderId(rs.getInt("order_id"));
		payInfo.setUserId(rs.getInt("user_id"));
		payInfo.setOrderPrice(rs.getInt("order_price"));
		payInfo.setVoucherId(rs.getInt("voucher_id"));
		payInfo.setPayStatus(rs.getInt("pay_status"));
		payInfo.setCreateDate(rs.getTimestamp("create_date"));
		payInfo.setCompleteDate(rs.getTimestamp("complete_date"));
		payInfo.setOp(Option.None);
		return payInfo;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}

}

